﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef  BaseUI_h
#define  BaseUI_h


#include "include\\Utils\\DrawingFunctions.h"
#include "include\\Utils\\defines01.h"


//#################################################################################################################
///// 
/////   :: РљР›РђРЎРЎ :: Р‘Р°Р·РѕРІС‹Р№ РєР»Р°СЃСЃ РґР»СЏ СЌР»РµРјРµРЅС‚РѕРІ UI
///// 
//#################################################################################################################
class CLREngi_UI_BaseUI
{
  protected:
	struct CUSTOMVERTEX
	{
		float x, y, z,w;//, rhw; // The transformed position for the vertex
		D3DCOLOR color;
	};

	#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE )

  public:
	LPDIRECT3DDEVICE9        *Device;
	D3DCOLOR                 BackColor,TextColor,BgColor;
	
	LPDIRECT3DVERTEXBUFFER9  *g_pVB;

	LPD3DXFONT               *Font; 
	LPD3DXFONT               *FontName; 

	// РїРѕР»РѕР¶РµРЅРёРµ РѕС‚РЅРѕСЃРёС‚РµР»СЊРЅРѕ РєР°РјРµСЂС‹

	int                      pos_x,              
		                     pos_y,              

							 pos_x_text,
		                     pos_y_text,
							 
							 width,
							 height;

	float                    rotate_x,
		                     rotate_y,
							 rotate_z;

	
	bool                     selected;
	bool                     allow_shift;

	CRLEngi_string           Name;
	CRLEngi_string           Text;

	POINT       last_mouse_point;
	POINT       curr_mouse_point;
	POINT       last_mouse_pitch;

	D3DXMATRIXA16          *matWorld;  
	D3DXMATRIX             Transform;
	 bool                     visible;
//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Render(){

	            (*Device)->SetTexture( 0, NULL );		
				(*Device)->SetStreamSource( 0, *g_pVB, 0, sizeof(CUSTOMVERTEX) );
				(*Device)->SetFVF( D3DFVF_CUSTOMVERTEX );
				(*Device)->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );				

				RECT destRect;				
				
				SetRect( &destRect, pos_x+2, pos_y,pos_x+width-5,pos_y+height); 			
				(*Font)->DrawText( NULL, Text.getChars(), -1, &destRect, 0|DT_CENTER|DT_WORDBREAK , TextColor );
				
				SetRect( &destRect, pos_x, pos_y-12,0,0); 
				(*FontName)->DrawText( NULL, Name.getChars(), -1, &destRect, DT_NOCLIP,  TextColor );	
								
				if ((!allow_shift)&&(selected)){
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y-1        ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y-1        , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y+height+1 ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y+height+1 , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y-1        ,0.0f),D3DXVECTOR3(pos_x-1       ,pos_y+height+1 , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x+width+1, pos_y-1        ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y+height+1 , 0.0f),Device);
				}
				else
				if (selected){
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y+1       -last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y-height+1 -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x+1       +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x-width-1+last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
				}
				Render_More();
}
void           Render_More(){}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool           MouseEvent(unsigned short event_index,POINT m){
	curr_mouse_point = m;
	MouseEvent_More(m);
	if (event_index==1)
	{
		
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))){
		selected = true;
		TextColor = D3DCOLOR_XRGB(255,255,255);
	
		last_mouse_point =m;
		last_mouse_pitch.x = pos_x+width-m.x;
        last_mouse_pitch.y = -(pos_y+height -m.y);
		allow_shift = true;

		return true;
	    }
	selected = false;
	
	TextColor = D3DCOLOR_XRGB(200,200,240);
	return false;
	}
	else
	if ((event_index==2)&&(allow_shift)&&(selected)){
	  pos_x +=m.x-last_mouse_point.x;
	  pos_y +=m.y-last_mouse_point.y;
	  allow_shift = false;
	  Init();		
	}
	//РґР»СЏ РјРёРіР°РЅРёСЏ
	else if (!selected){
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))&&(!allow_shift)){
	BgColor = D3DCOLOR_XRGB(91,91,91);
	Init();
	}else{BgColor = D3DCOLOR_XRGB(21,21,21);
	Init();
	}}
	else if (selected)BgColor = D3DCOLOR_XRGB(51,51,51) ;
	return false;
}
void           MouseEvent_More(POINT m){}

void   Setup_SizePos(int x,int y,int w,int h){
	pos_x  = x;
	pos_y  = y;
	width  = w;
	height = h;
	Resize();
}
void Setup_Color(D3DCOLOR  Back, D3DCOLOR Text, D3DCOLOR Bg){
    BackColor = Back;
    TextColor = Text;
    BgColor   = Bg;
}
void SetText(char *text){Text=text;}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT        Init() {
	
	SAFE_RELEASE(g_pVB);
	g_pVB = new LPDIRECT3DVERTEXBUFFER9();

   CUSTOMVERTEX vertices[] =
    {
		{ pos_x        , pos_y+height    , 0.0f, 1.0f, BgColor   }, 
        { pos_x        , pos_y           , 0.0f, 1.0f, BgColor	 }, 
        { pos_x+width  , pos_y+height    , 0.0f, 1.0f, BgColor	 },
		{ pos_x+width  , pos_y           , 0.0f, 1.0f, BgColor	 },
    };

    if( FAILED( (*Device)->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

	VOID* pVertices;
    if( FAILED( (*g_pVB)->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof(vertices) );
	(*g_pVB)->Unlock();

	
Init_More();
}
HRESULT        Resize() {
	if (g_pVB!=NULL){	
	              
		CUSTOMVERTEX vertices[] =
			{
				{ pos_x        , pos_y+height    , 0.0f, 1.0f, BgColor  }, 
				{ pos_x        , pos_y           , 0.0f, 1.0f, BgColor	 }, 
				{ pos_x+width  , pos_y+height    , 0.0f, 1.0f, BgColor	 },   
				{ pos_x+width  , pos_y           , 0.0f, 1.0f, BgColor	 },
			};

		VOID* pVertices;
		if( FAILED( (*g_pVB)->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
			return E_FAIL;
		memcpy( pVertices, vertices, sizeof(vertices) );
		(*g_pVB)->Unlock();

	}else Init();
}
void           Init_More(){}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Select(){	selected = true;	TextColor = D3DCOLOR_XRGB(255,255,255);}
void           Deselect(){selected = false;   TextColor = D3DCOLOR_XRGB(200,200,240);}

//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                     РљРћРќРЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLREngi_UI_BaseUI(LPDIRECT3DDEVICE9 *D,char *nm){
   
   pos_x  = 0;
   pos_y  = 0;
   width  = 5;
   height = 15;
   Device = D;
   g_pVB=NULL;

   Font=NULL; 
   selected = false;
   TextColor =D3DCOLOR_XRGB(200,200,240);


   Name = nm;

   allow_shift = false;
   BgColor = D3DCOLOR_XRGB(51,51,51);
   BackColor = D3DCOLOR_XRGB(200,200,200);

   //SAFE_RELEASE(FontName);
   FontName = new LPD3DXFONT();
   D3DXCreateFont( (*Device), 10,5, FW_ULTRALIGHT, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), 
		                 FontName );
   
 //  SAFE_RELEASE(Font);
	Font = new LPD3DXFONT();
   D3DXCreateFont( (*Device), 15,6, FW_BOLD, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Courier "), 
		                 Font );
   
   visible = true;

   Init();
   }
CLREngi_UI_BaseUI(){


};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                       Р”Р•РЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~CLREngi_UI_BaseUI(void)
   {
	   SAFE_RELEASE(g_pVB);
	   SAFE_RELEASE(Font);
	   SAFE_RELEASE(FontName);
   }

};

#endif 

